<?php
namespace app\user\controller;
use think\Input;
class Index extends Common{
    public function initialize(){
        parent::initialize(); 
        if($this->userInfo['level']==1){ 
            $this->redirect('login/index');
        }
        $this->assign('agentRes',cache('agentRes'));
        $this->assign('shoplist',cache('shoplist'));   
        $this->assign('cionlist',cache('cionlist')); 
    }
    public function index(){
        if($this->userInfo['level']!=1){
            $this->assign('title','【代理】下级代理');
            $userlisturl=url('user/agent/index');
            $this->assign('dailinav','<li><a href="'.$userlisturl.'" target="main_pan"><i class="layui-icon">&#xe66f;</i> 下级码商</a></li>'); 
            $where['agentid']=$this->userInfo['id'];
            $agent['open']=db('users')->where('is_lock',0)->where($where)->count();
            $agent['lock']=db('users')->where('is_lock',1)->count();
            $this->assign('agentinfo',$agent);
            if(request()->isPost()){ 
                $page =input('page')?input('page'):1;
                $pageSize =input('limit')?input('limit'):config('pageSize');
                $list=db('users')->alias('u')
                    ->join(config('database.prefix').'user_level ul','u.level = ul.level_id','left')
                    ->field('u.*,ul.level_name')
                    ->where('u.mobile|u.username','like',"%".$key."%")->where($where)
                    ->order('u.id desc')
                    ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                    ->toArray();
                foreach ($list['data'] as $k=>$v){
                    $list['data'][$k]['reg_time'] = date('Y-m-d H:i',$v['reg_time']);
                }
                return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
            }
        }else{
            $this->assign('title','下级会员');
            $this->assign('dailinav',NULL); 
        }  
        return $this->fetch();
    }  
    //设置会员状态
    public function usersState(){
        return ['status'=>-1,'msg'=>'请联系管理员开通!'];
        // $id=input('post.userid');
        // $is_lock=input('post.is_lock');
        // if(db('users')->where('id='.$id)->update(['is_lock'=>$is_lock])!==false){
        //     return $result = ['status'=>1,'msg'=>'设置成功!'];
        // }else{
        //     return $result = ['status'=>0,'msg'=>'设置失败!'];
        // }
    }
    // 代理订单
    public function orderlist(){
        if(request()->isPost()){   
            $where['o.agentid']=session('user.id');
            $map='';
            $shopid = input('post.shopid');
            if($shopid) $where['shopid']=$shopid;
            $cionid = input('post.cionid');
            if($cionid) $where['cionid']=$cionid;
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1])+86399;    
                $map= [$starttime,$endtime];  
            }
            $timequan = input('post.timequan');
            if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid;  
            $channelid = input('post.channelid');
            if($channelid) $where['channelid']=$channelid; 
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;    

            $state = input('post.state');
            if($state == -1){
                if($state) $where['state']=0;
            }else{
                if($state) $where['state']=$state;
            }
            $points = input('post.points');
            if($points)$where['points']=$points;   
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('channel_order')->alias('o')
                ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
                ->field('o.*,ub.name as payname,ub.bank_no,ub.bank_name')
                ->where($where)->whereTime('o.addtime',$map)->order('o.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();   
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                if($v['paytime']) $list['data'][$k]['paytime'] = date('Y-m-d H:i',$v['paytime']);
                if($v['state']===0){  
                    $list['data'][$k]['state_name'] ='<span class="red">待支付</span>';
                }else if($v['state']===1){
                    $list['data'][$k]['state_name'] ='<span class="blue">待确认</span>';
                }else if($v['state']===2){
                    $list['data'][$k]['state_name'] ='<span class="gre">已确认</span>';
                }else if($v['state']===3){
                    $list['data'][$k]['state_name'] ='<span class="hui">已取消</span>';
                } 
                if($v['points']===0){ 
                    $list['data'][$k]['points_name'] ='<span class="red">待上分</span>';
                }else if($v['points']===1){
                    $list['data'][$k]['points_name'] ='<span class="gre">自动上分</span>';
                }else if($v['points']===2){
                    $list['data'][$k]['points_name'] ='<span class="gre">手动上分</span>';
                }
                if($v['fei_agent_jie']===0){
                    $list['data'][$k]['fei_agent'] ='<span class="red" title="待结算">'.$v['fei_agent'].'</span>';
                }else if($v['fei_user_jie']===1){
                    $list['data'][$k]['fei_agent'] ='<span class="gre" title="已结算">'.$v['fei_agent'].'</span>';
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        } 
        
        // 订单汇总
        // $payorder=[];
        // $payorder['no']=db('channel_order')->where('agentid',session('user.id'))->where('state',0)->sum('money');
        // $payorder['que']=db('channel_order')->where('agentid',session('user.id'))->where('state',1)->sum('money');
        // $payorder['yes']=db('channel_order')->where('agentid',session('user.id'))->where('state',2)->sum('money'); 
        // $this->assign('payorder',$payorder);
        if(!$agentRes){
           $agentRes = db('users')->where('agentid',session('user.id'))->select();
           cache('agentRes',$agentRes);
        }
        $this->assign('title','订单管理');
        return $this->fetch();
    } 

    public function total(){
        if(request()->isPost()){ 
            $where=[];
            $map='';
            $agentid=session('user.id');
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]);
                if($starttime >= $endtime){
                    return $result = ['code'=>-1,'msg'=>'开始日期不能大于结束日期!','data'=>'','rel'=>1];
                }     
                $map= [$starttime,$endtime];  
            }
            $state = input('post.state');
            if($state == -1){
                if($state) $where['state']=0;
            }else{
                if($state) $where['state']=$state;
            }
            if(input('post.userid')){
                $where['userid']=input('post.userid');
            }else{
                $where['agentid']=session('user.id');
            }   
            if(input('post.userid')){
                $wheres['userid']=input('post.userid');
            }else{
                $wheres['agentid']=session('user.id');
            }   
            $orderid = input('post.orderid');
            if($orderid) $where['o.orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['o.orderid_out']=$orderid_out; 
            
            $s_today = strtotime(date('Y-m-d',time()));
            $e_today = strtotime(date('Y-m-d',time()))+86399;
            if($agentid){

                $payorder['total']=db('channel_order')->where('state',2)->whereTime('addtime',$map)->where($where)->sum('money');
                $payorder['num']=count(db('channel_order')->where('state',2)->whereTime('addtime',$map)->where($where)->select());
                $payorder['a_money']=db('channel_order')->where('state',2)->whereTime('addtime',$map)->where($where)->sum('fei_agent');
                $payorder['u_money']=db('channel_order')->where('state',2)->whereTime('addtime',$map)->where($where)->sum('fei_user');

                //今日统计
                $payorder['today_total']=db('channel_order')->where('state',2)->whereTime('addtime',[$s_today,$e_today])->where($wheres)->sum('money');
                $payorder['today_num']=count(db('channel_order')->where('state',2)->whereTime('addtime',[$s_today,$e_today])->where($wheres)->select());
                $payorder['today_a_money']=db('channel_order')->where('state',2)->whereTime('addtime',[$s_today,$e_today])->where($wheres)->sum('fei_agent');
                $payorder['today_u_money']=db('channel_order')->where('state',2)->whereTime('addtime',[$s_today,$e_today])->where($wheres)->sum('fei_user');
                $payorders[0]=$payorder;
                return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$payorders,'rel'=>1];
            }else{
                return $result = ['code'=>1,'msg'=>'获取失败，缓存失败!','data'=>$payorders,'rel'=>1];
            }
       
        } 
    }   
} 